package com.aptemo.webetek.client.widgets.dialogs;

import com.aptemo.webetek.client.events.DecisionEvent;
import com.aptemo.webetek.client.events.DecisionHandler;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;

public class DecisionDialog
		extends DialogBox implements ClickHandler, HasDecisionHandlers
{

	private Button	decisionRightButton	= new Button("Yes");
	private Button	decisionLeftButton	= new Button("No");
	private Label	infoField			= new Label("");
	private String	id;

	HTML			longQuestion		= new HTML("<empty>");

	public DecisionDialog(String shortQuestion)
	{
		super();
		setText(shortQuestion);

		FlexTable layout = new FlexTable();
		layout.getFlexCellFormatter().setColSpan(0, 0, 2);
		layout.setWidget(0, 0, infoField);
		layout.getFlexCellFormatter().setColSpan(1, 0, 2);
		layout.setWidget(1, 0, longQuestion);
		layout.setWidget(2, 0, decisionLeftButton);
		layout.setWidget(2, 1, decisionRightButton);

		add(layout);

		decisionLeftButton.addClickHandler(this);
		decisionRightButton.addClickHandler(this);
	}

	public void setQuestionString(String question)
	{
		longQuestion.setText(question);
	}

	@Override
	public void onClick(ClickEvent event)
	{
		if (event.getSource().equals(decisionLeftButton))
		{
			hide();
		}
		else if (event.getSource().equals(decisionRightButton))
		{
			DecisionEvent de = new DecisionEvent(infoField, id);
			fireEvent(de);
		}

	}

	@Override
	public HandlerRegistration addDecisionHandler(DecisionHandler handler)
	{
		return addHandler(handler, DecisionEvent.TYPE);
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	@Override
	public void center()
	{
		super.center();
		infoField.setText("");
	}

}
